package com.cwj.thread;

import java.io.IOException;
import java.net.ServerSocket;

/**
 * @author chenwujie
 * @date 2021-02-25 13:31
 */
public class InterruptTest extends Thread{
    private static final Object obj = new Object();
    volatile ServerSocket socket;
    public static void main(String[] args) {
//        Thread thread = new Thread() {
////            @Override
////            public void run() {
////                synchronized (obj){
////                    for (int i = 0; i < 10000; i++) {
////                        if(i > 100){
////                            try {
////                                obj.wait();
////                            } catch (InterruptedException e) {
////                                e.printStackTrace();
////                                break;
////                            }
////                        }
////                        if(isInterrupted()){
////                            System.out.println("Thread isInterrupted");
////                            break;
////                        }
////                        System.out.println(i);
////                    }
////                }
////            }
//
//            @Override
//            public void run() {
//
//                while (!Thread.currentThread().isInterrupted()) {
//                    System.out.println("Thread running...");
//                    try {
//                        /*
//                         * 如果线程阻塞，将不会去检查中断信号量stop变量，所 以thread.interrupt()
//                         * 会使阻塞线程从阻塞的地方抛出异常，让阻塞线程从阻塞状态逃离出来，并
//                         * 进行异常块进行 相应的处理
//                         */
//                        Thread.sleep(1000);// 线程阻塞，如果线程收到中断操作信号将抛出异常
//                    } catch (InterruptedException e) {
//                        System.out.println("Thread interrupted...");
//                        /*
//                         * 如果线程在调用 Object.wait()方法，或者该类的 join() 、sleep()方法
//                         * 过程中受阻，则其中断状态将被清除
//                         */
//                        System.out.println(this.isInterrupted());// false
//
//                        //中不中断由自己决定，如果需要真真中断线程，则需要重新设置中断位，如果
//                        //不需要，则不用调用
//                        Thread.currentThread().interrupt();
//                    }
//                }
//                System.out.println("Thread exiting under request...");
//
//            }
//
//        };
        InterruptTest thread = new InterruptTest();

        thread.start();

        try {
            Thread.sleep(3_000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        thread.interrupt();
        try {
            thread.socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("end");

//        try {
//            Thread.sleep(1_000L);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        synchronized (obj){
//            obj.notifyAll();
//        }


        Thread thread1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        System.out.println("响应中断");
                    }
                    System.out.println("继续运行: " + i);
                }
            }
        };

        thread1.start();
        thread1.interrupt();

    }

    @Override
    public void run() {
        try {
            socket = new ServerSocket(8888);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        while (!interrupted()){
            System.out.println("waiting connection ...");
            try {
                socket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("socket accept Error");
                interrupt();
            }
        }
    }
}
